package com.qys.livesMall.shop.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.shop.enums.ShopMessageEnum;
import com.qys.livesMall.shop.modal.dto.H2DataPackageDTO;
import com.qys.livesMall.shop.modal.dto.QueryDataPackageDTO;
import com.qys.livesMall.shop.modal.entity.H2DataPackage;
import com.qys.livesMall.shop.modal.vo.H2DataPackageVO;
import com.qys.livesMall.shop.service.H2DataPackageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 流量包表(H2DataPackage)表控制层
 *
 * @author makejava
 * @since 2023-06-20 13:43:49
 */
@RestController
@RequestMapping("/sys/dataPackage")
@Tag(name = "sys-流量后台管理", description = "sys-流量后台管理")
public class H2DataPackageController {


    @Autowired
    private H2DataPackageService h2DataPackageService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/getAll")
    @Operation(summary = "分页列表查询")
    public R<IPage<H2DataPackageVO>> getAll(@RequestBody QueryDataPackageDTO dto) {
        LambdaQueryWrapper<H2DataPackage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(H2DataPackage::getIsDelete, 0).eq(H2DataPackage::getDataType, dto.getDataType());

        IPage<H2DataPackageVO> page = h2DataPackageService.page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper).convert(e -> {
            H2DataPackageVO packageVO = new H2DataPackageVO();
            BeanUtils.copyProperties(e, packageVO);
            packageVO.setPackagePrice(e.getPackagePrice().toString());
            return packageVO;
        });
        return R.ok(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    @Operation(summary = "根据ID查询数据")
    public R<H2DataPackageVO> selectOne(@RequestParam Serializable id) {

        H2DataPackage dataPackage = h2DataPackageService.getById(id);
        if (null == dataPackage) {
            return R.ok();
        }

        H2DataPackageVO packageVO = new H2DataPackageVO();
        BeanUtils.copyProperties(dataPackage, packageVO);
        packageVO.setPackagePrice(dataPackage.getPackagePrice().toString());
        return R.ok(packageVO);
    }

    /**
     * 新增/编辑数据
     *
     * @param dto 实体对象
     * @return 新增结果
     */
    @PostMapping("/edit")
    @Operation(summary = "新增/编辑数据")
    public R<?> edit(@RequestBody H2DataPackageDTO dto) {

        //重复性校验
        if(StringUtils.isBlank(dto.getId())){
            LambdaQueryWrapper<H2DataPackage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(H2DataPackage::getPackageName, dto.getPackageName())
                    .or()
                    .eq(H2DataPackage::getPackageContent, dto.getPackageContent());
            long count = h2DataPackageService.count(queryWrapper);
            if(count > 0){
                throw new QysException(ShopMessageEnum.DATA_PACKAGE_EXIST);
            }
        }

        H2DataPackage h2DataPackage = new H2DataPackage();
        BeanUtils.copyProperties(dto, h2DataPackage);
        h2DataPackage.setId(StringUtils.isBlank(dto.getId()) ? null : Long.parseLong(dto.getId()));
        h2DataPackage.setPackagePrice(new BigDecimal(dto.getPackagePrice()));

        h2DataPackageService.saveOrUpdate(h2DataPackage);
        return R.ok();
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/delete")
    @Operation(summary = "删除数据")
    public R<?> delete(@RequestBody List<String> idList) {
        h2DataPackageService.removeByIds(idList);
        return R.ok();
    }
}

